package org.uned.agonzalo16.bitacora.web.article;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.uned.agonzalo16.bitacora.dao.ArticleDao;
import org.uned.agonzalo16.bitacora.service.security.AuthenticatedUser;
import org.uned.agonzalo16.bitacora.service.security.AuthenticationProvider;
import org.uned.agonzalo16.bitacora.web.comment.CommentForm;

@RequestMapping("/article")
@Controller
public class ArticleController {

	@Autowired
	private ArticleDao articleDao;

	@Autowired
	private AuthenticationProvider authenticationProvider;

	@RequestMapping(method = RequestMethod.GET, value = "/{id}")
	public String show(@PathVariable("id") Long id, Model model) {
		model.addAttribute("article", articleDao.get(id));

		AuthenticatedUser user = authenticationProvider.getCurrentUserDetails();

		if (user == null) {
			return "article/show";
		} else {
			model.addAttribute("comment", new CommentForm(id));
			return "article/showAndComments";
		}
	}
}
